

namespace Stee.CAP8.MessageBus.Kafka.Utility
{
    using System;
    using Stee.CAP8.MessageBus.Interface.Models;

    internal static class ValidateHelper
    {
        /// <summary>
        /// Validate topic
        /// </summary>
        /// <param name="topicName">Topic name</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        internal static bool ValidateTopic(TopicName topicName)
        {
            string topic = topicName?.Name.Trim();

            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentNullException($"Null or empty message topic encountered: {nameof(topicName)}");
            }

            return true;
        }

        /// <summary>
        /// Validate group ID
        /// </summary>
        /// <param name="groupID">Group ID</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        internal static bool ValidateGroup(string groupID)
        {
            string grp = groupID?.Trim();

            if (string.IsNullOrEmpty(grp))
            {
                throw new ArgumentNullException($"Null or empty group ID encountered: {nameof(groupID)}");
            }

            return true;
        }

        /// <summary>
        /// Validate message
        /// </summary>
        /// <typeparam name="TMessage">Message data type</typeparam>
        /// <param name="message">Published or incoming subscribed message</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        internal static bool ValidateMessage<TMessage>(TMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException($"Null message encountered: {nameof(message)}");
            }
            else if (message.GetType() == typeof(string) && string.IsNullOrEmpty(message.ToString()))
            {
                throw new ArgumentNullException($"Null or empty message encountered: {nameof(message)}");
            }

            return true;
        }

        /// <summary>
        /// Validate command message
        /// </summary>
        /// <param name="commandMsg">Command message</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        internal static bool ValidateCommand(CommandMessage commandMsg)
        {
            if (commandMsg == null)
            {
                throw new ArgumentNullException($"Null command message encountered: {nameof(commandMsg)}");
            }
            //else if (string.IsNullOrEmpty(commandMsg.Action))
            //{
            //    throw new ArgumentNullException($"Null command reply topic encountered: {nameof(commandMsg.Action)}");
            //}
            else if (commandMsg.CreateDT == null)
            {
                throw new ArgumentNullException($"Null command create date-time encountered: {nameof(commandMsg.CreateDT)}");
            }
            else if (commandMsg.Data == null)
            {
                throw new ArgumentNullException($"Null command data encountered: {nameof(commandMsg.Data)}");
            }

            return true;
        }
    }
}
